﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADO.NET.Samples.Common;
using ADO.NET.Samples.DataSets;
using ADO.NET.Samples.DataSets.DBDataSetTableAdapters;

namespace ADO.NET.Samples.Forms.LINQ
{
    [FormDescription("LINQ to Typed DataSet Data Binding", "Demonstrate how to use LINQ to query data from typed DataSet and bind data to DataGridView.")]
    public partial class LINQtoTypedDataSetDataBinding : Form
    {
        public LINQtoTypedDataSetDataBinding()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Data binding
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LINQtoTypedDataSetDataBinding_Load(object sender, EventArgs e)
        {
            DBDataSet ds = new DBDataSet();
            ArticleTableAdapter articleAdapter = new ArticleTableAdapter();
            UserTableAdapter userAdapater = new UserTableAdapter();
            articleAdapter.Fill(ds.Article);
            userAdapater.FillUsers(ds.User);
            var articles = from article in ds.Article
                           select article;
            var users=from user in ds.User select user;
            //Bind data to datagridview
            this.dataGridView1.DataSource = articles.AsDataView();
            this.listBox1.DisplayMember = "UserName";
            this.listBox1.ValueMember = "UserID";
            this.listBox1.DataSource = users.CopyToDataTable();
        }
    }
}
